package com.mbyte.easy.rest.video;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mbyte.easy.admin.entity.Video;
import com.mbyte.easy.admin.service.IVideoService;
import com.mbyte.easy.common.controller.BaseController;
import com.mbyte.easy.common.web.AjaxResult;
import com.mbyte.easy.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
* <p>
* 前端控制器
* </p>
* @author lp
* @since 2019-03-11
*/
@RestController
@RequestMapping("rest/video")
public class RestVideoController extends BaseController  {

    @Autowired
    private IVideoService videoService;

    /**
    * 查询列表
    *
    * @param pageNo
    * @param pageSize
    * @param video
    * @return
    */
    @RequestMapping
    public AjaxResult index(@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,@RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize, String submitTimeSpace, String allotTimeSpace, String createTimeSpace, String updateTimeSpace, Video video) {
        Page<Video> page = new Page<Video>(pageNo, pageSize);
        QueryWrapper<Video> queryWrapper = new QueryWrapper<Video>();

        if(video.getDraftId() != null  && !"".equals(video.getDraftId() + "")) {
            queryWrapper = queryWrapper.like("draft_id",video.getDraftId());
         }


        if(video.getDraftType() != null  && !"".equals(video.getDraftType() + "")) {
            queryWrapper = queryWrapper.like("draft_type",video.getDraftType());
         }


        if(video.getVideoUrl() != null  && !"".equals(video.getVideoUrl() + "")) {
            queryWrapper = queryWrapper.like("video_url",video.getVideoUrl());
         }


        if(video.getVideoLength() != null  && !"".equals(video.getVideoLength() + "")) {
            queryWrapper = queryWrapper.like("video_length",video.getVideoLength());
         }


        if(video.getSubmitTime() != null  && !"".equals(video.getSubmitTime() + "")) {
            queryWrapper = queryWrapper.like("submit_time",video.getSubmitTime());
         }


        if(video.getVideoMaker() != null  && !"".equals(video.getVideoMaker() + "")) {
            queryWrapper = queryWrapper.like("video_maker",video.getVideoMaker());
         }




        if(video.getIsDelete() != null  && !"".equals(video.getIsDelete() + "")) {
            queryWrapper = queryWrapper.like("is_delete",video.getIsDelete());
         }


        if(video.getAllotTime() != null  && !"".equals(video.getAllotTime() + "")) {
            queryWrapper = queryWrapper.like("allot_time",video.getAllotTime());
         }

        IPage<Video> pageInfo = videoService.page(page, queryWrapper);

        Map<String, Object> map = new HashMap<>();
        map.put("searchInfo",  video);
        map.put("pageInfo", new PageInfo(pageInfo));

        return this.success(map);
    }


    /**
    * 添加
    * @param video
    * @return
    */
    @PostMapping("add")
    public AjaxResult add(Video video){
        return toAjax(videoService.save(video));
    }

    /**
    * 添加
    * @param video
    * @return
    */
    @PostMapping("edit")
    public AjaxResult edit(Video video){
        return toAjax(videoService.updateById(video));
    }
    /**
    * 删除
    * @param id
    * @return
    */
    @GetMapping("delete/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        return toAjax(videoService.removeById(id));
    }
    /**
    * 批量删除
    * @param ids
    * @return
    */
    @PostMapping("deleteAll")
    public AjaxResult deleteAll(@RequestBody List<Long> ids){
        return toAjax(videoService.removeByIds(ids));
    }

}

